﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NamePanel : BasePanel<NamePanel>
{
    public CustomGUIInput inputName;
    public CustomGUIButton btnRandName;
    public CustomGUIButton btnStartGame;
    public CustomGUIButton btnBackMenu;
    void Start()
    {
        Hide();
        btnRandName.clickEvent += () =>
        {
            inputName.SetText(RandName(6));
        };
        btnStartGame.clickEvent += () =>
        {
            string name = inputName.content.text;
            GameDataMgr.Instance.SaveNickName(name);
            if (name.Length == 0)
            {
                Hide();
                ErrorPanel.Instance.SetRecover(delegate()
                {
                    NamePanel.Instance.Show();
                });
                ErrorPanel.Instance.Show();
                return;
            }
            SceneManager.LoadScene("Game");
        };
        btnBackMenu.clickEvent += () =>
        {
            Hide();
            StartPanel.Instance.Show();
        };
    }

    string RandName(int len)
    {
        string result = "";
        for (int i = 0; i < len; i++)
        {
            
            char c = (char)('a' + Random.Range(0, 26));
            result += c.ToString();
        }
        return result;
    }

    public override void Show()
    {
        inputName.SetText(RandName(6));
        base.Show();
    }
}
